
import HeadPrefab from "../../../component/HeadPrefab";
import DateUtil from "../../../engine/DateUtil";
import { EventManager } from "../../../engine/EventManager";
import { Logger } from "../../../engine/Logger";
import { SpriteFrameComponent } from "../../../engine/SpriteFrameComponent";
import JhaoScrollViewLayout from "../../../engine/ui/scrollview/JhaoScrollViewLayout";
import UIManager from "../../../engine/UIManager";
import ViewBase from "../../../engine/ViewBase";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { SocketClient } from "../../../net/SocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { EventConfig } from "../../config/EventConfig";
import { GameConfig } from "../../config/GameConfig";
import { ViewConfig } from "../../config/ViewConfig";
import RedDotManager from "../../manager/RedDotManager";
import { ClubShenheModel } from "../../model/ClubShenheModel";
import { RoomModel } from "../../model/RoomModel";
import { RoomPlayer } from "../../model/RoomPlayer";
import { RoomTemplateModel } from "../../model/RoomTemplateModel";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubHall extends ViewBase {

    // private isShowLeft: boolean = true
    private lastGetTime: number = 0
    private leftNode: cc.Node
    // private pageNode:c
    private txtPage: cc.Label;
    private page: number = 1;
    // private pageSize: number = 6;
    private pageSize: number = 8;
    private totalPage: number = 0
    private lastClickTime: number = 0
    private btnNext: cc.Node;
    private btnPre: cc.Node

    private deskItemLayout: JhaoScrollViewLayout;

    onEnable(): void {
        super.onEnable()
        this.deskItemLayout = this.getChild(this.node, "scrollView/view/content", JhaoScrollViewLayout)

        this.txtPage = this.getChild(this.node, "txtPage", cc.Label)

        this.leftNode = this.findChild(this.node, "leftNode")
        EventManager.instance.on(EventConfig.Event_Update_Club_Room, this.onGetAllDesk, this)
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        this.btnNext = btnNext;
        this.btnPre = btnPre
        UIHelper.onAddEvent(btnNext, () => {
            if (this.page < this.totalPage) {
                this.page++
                // this.onGetAllDesk()
            }
        })
        UIHelper.onAddEvent(btnPre, () => {
            if (this.page > 1) {
                this.page--
                // this.onGetAllDesk()
            }
        })

        this.refreshLeftNode()
        // this.onGetAllDesk()
        if (UserModel.instance.nowClub.actor != ClubConfig.Club_Actor_Type.general) {
            ClubSocketClient.req_club_apply_list(UserModel.instance.nowClub.clubid, (dataList: Array<object>) => {
            })
        }
        this.getClubRoom()
    }

    protected update(dt: number): void {
        this.getClubRoom()
    }

    private getClubRoom() {
        if (DateUtil.now() - this.lastGetTime >= 1000) {
            this.lastGetTime = DateUtil.now()
            if (UserModel.instance.nowClub) {
                ClubSocketClient.req_club_room(UserModel.instance.nowClub.clubid, (respPack: JhaoProto.resp_club_room) => {
                    respPack.template.sort((a: JhaoProto.RoomTemplate, b: JhaoProto.RoomTemplate) => {
                        return a.gameType - b.gameType
                    })
                    this.onGetAllDesk(respPack)
                })
            }
        }
    }

    private onGetAllDesk(protoPack: JhaoProto.resp_club_room) {
        // this.scrollView.scrollToTop();
        let allRoomList: Array<RoomModel> = RoomModel.clubRoomList;
        let noFullRoomList: Array<RoomModel> = []
        let fullRoomList: Array<RoomModel> = []
        for (let i = 0; i < allRoomList.length; i++) {
            let room: RoomModel = allRoomList[i]
            // if (UserModel.instance.clubShowGameType == 0 || UserModel.instance.clubShowGameType == room.gameType) {
            //     if (room.getPlayerNum() >= room.conf.renshu) {
            //         fullRoomList.push(room)
            //     } else {
            //         noFullRoomList.push(room)
            //     }
            // }
        }
        // let templateList: Array<RoomTemplateModel> = RoomTemplateModel.getClubListByGameType(UserModel.instance.clubShowGameType)
        // templateList.sort((a: RoomTemplateModel, b: RoomTemplateModel) => {
        //     return a.gameType - b.gameType
        // })
        // Logger.log("templateList===", templateList)
        let allList: Array<JhaoProto.IRoomTemplate> = []
        // allList = allList.concat(noFullRoomList)
        allList = allList.concat(protoPack.template)
        // allList = allList.concat(fullRoomList)
        // Logger.log("allList===", allList)
        this.totalPage = allList.length % this.pageSize == 0 ? allList.length / this.pageSize : Math.floor(allList.length / this.pageSize) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        // Logger.log("totalPage===", this.totalPage)
        this.txtPage.string = this.page + "/" + this.totalPage
        if (this.page > this.totalPage) {
            this.page = 1
        }
        this.btnPre.active = true
        this.btnNext.active = true
        if (this.page <= 1) {
            this.btnPre.active = false
        }
        if (this.page >= this.totalPage) {
            this.btnNext.active = false
        }

        this.refreshDesk(allList)
    }

    private refreshLeftNode() {
        // let leftNode: cc.Node = this.leftNode
        // for (let i = 0; i < 1; i++) {
        //     let btnGame: cc.Node = leftNode.getChildByName("btnGame" + i)
        //     btnGame.active = this.isShowLeft
        //     if (i == UserModel.instance.clubShowGameType) {
        //         btnGame.getComponent(cc.Sprite).spriteFrame = btnGame.getComponent(SpriteFrameComponent).spriteFrameList[1]
        //     } else {
        //         btnGame.getComponent(cc.Sprite).spriteFrame = btnGame.getComponent(SpriteFrameComponent).spriteFrameList[0]
        //     }
        //     UIHelper.onAddEvent(btnGame, () => {
        //         UserModel.instance.clubShowGameType = i
        //         this.refreshLeftNode();
        //         this.page = 1
        //         this.onGetAllDesk()
        //     })
        // }
        // let btnRight: cc.Node = this.findChild(leftNode, "btnRight")
        // btnRight.active = !this.isShowLeft;
        // UIHelper.onAddEvent(btnRight, () => {
        //     this.isShowLeft = true;
        //     this.leftNode.x = -547
        //     this.refreshLeftNode()
        // })
        // let btnLeft: cc.Node = this.findChild(leftNode, "btnLeft")
        // btnLeft.active = this.isShowLeft;
        // UIHelper.onAddEvent(btnLeft, () => {
        //     this.isShowLeft = false
        //     this.leftNode.x = -728
        //     this.refreshLeftNode()
        // })
    }

    private refreshDesk(allList: Array<JhaoProto.IRoomTemplate>) {
        this.deskItemLayout.total(allList.length, allList)
    }

    private refreshDeskItem(item: cc.Node, layerNodeList: Array<cc.Node>, index: number, allList: Array<JhaoProto.IRoomTemplate>) {
        let model: JhaoProto.IRoomTemplate | RoomModel = allList[index]
        if (model) {
            let lbName: cc.Label = this.getChild(layerNodeList[1], "lbName", cc.Label)
            if (model.name) {
                lbName.string = model.name
            } else {
                lbName.string = ""
            }
            let txtGameName: cc.Label = this.getChild(layerNodeList[1], "txtGameName", cc.Label)
            txtGameName.node.active = true
            txtGameName.string = "" + model.roomConfigBase.renshu + "人" + GameConfig.GameNameList[model.gameType]
            let txtJushu: cc.Label = this.getChild(layerNodeList[1], "txtJushu", cc.Label)
            txtJushu.node.active = true
            txtJushu.string = ""
            if (model instanceof RoomModel) {
                if (model.num_of_turns >= 1) {
                    txtJushu.string = "已开局" + model.num_of_turns + "/" + model.conf.jushu;
                } else {
                    txtJushu.string = "等待中"
                }
            }
            let lbNameList: Array<cc.Label> = []
            for (let i = 0; i < 5; i++) {
                lbNameList[i] = this.getChild(layerNodeList[1], "lbName" + i, cc.Label)
                lbNameList[i].string = ""
            }

            UIHelper.onAddEvent(item, () => {
                Logger.log("点击桌子==", model)
                if (model instanceof RoomModel) {
                    if (DateUtil.now() - this.lastClickTime >= 1000) {
                        this.lastClickTime = DateUtil.now()
                        if (RoomPlayer.getPlayerNum(model.playerList) < model.conf.renshu) {
                            // SocketClient.req_join_room(model.room_id)
                        } else {
                            // UIManager.instance.showView(ViewConfig.viewConfig.ViewClubRoomDetail, null, model)
                        }
                    }
                } else {
                    UIManager.instance.showView(ViewConfig.viewConfig.ViewClubRoomDetail, null, model)
                }
            })

            let desk_poker: cc.Node = this.findChild(layerNodeList[0], "desk_poker")
            let desk_mj: cc.Node = this.findChild(layerNodeList[0], "desk_mj")
            if (model.gameType == GameConfig.Game_Type.ddz) {
                desk_poker.active = true
                desk_mj.active = false
                this.refreshDeskOne(desk_poker, model, lbNameList)
            }
            else {
                desk_poker.active = false
                desk_mj.active = true
                this.refreshDeskOne(desk_mj, model, lbNameList)
            }

        }



    }

    private refreshDeskOne(deskNode: cc.Node, model: JhaoProto.IRoomTemplate, lbNameList: Array<cc.Label>) {
        let renshu: number = model.roomConfigBase.renshu
        this.hideAllChildren(deskNode)
        let childName: string = "chairNode" + renshu;
        let chairNode: cc.Node = deskNode.getChildByName(childName)
        chairNode.active = true;
        for (let i = 0; i < renshu; i++) {
            let btnJoinDesk: cc.Node = this.findChild(chairNode, "btnJoinDesk" + i)
            btnJoinDesk.active = false
            let head: HeadPrefab = this.getChild(chairNode, "head" + i, HeadPrefab)
            head.node.active = false
        }
        if (model instanceof RoomModel) {
            for (let i = 0; i < renshu; i++) {
                let btnJoinDesk: cc.Node = this.findChild(chairNode, "btnJoinDesk" + i)
                let head: HeadPrefab = this.getChild(chairNode, "head" + i, HeadPrefab)
                if (model.num_of_turns >= 1) {
                    btnJoinDesk.active = false
                } else {
                    btnJoinDesk.active = true
                }
                let player: RoomPlayer = model.playerList[i]
                if (player) {
                    head.node.active = true
                    head.setHead(player.headimg, player.lv)
                    lbNameList[i].string = player.nick_name
                    lbNameList[i].node.setPosition(head.node.x, head.node.y - 32)
                    // let txtName: cc.Label = this.getChild(head.node, "txtName", cc.Label)
                    // txtName.string = player.nick_name;
                }
            }
        }

    }


}
